package com.orphan.marketBack.controller;

import com.orphan.marketBack.service.InGoodsService;
import com.orphan.marketBack.service.StockService;
import com.orphan.marketBack.vo.ResultVO;
import com.orphan.util.dto.GoodsDto;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StockController {
    @Autowired
    private StockService stockService;
    @Autowired
    private InGoodsService inGoodsService;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/getStockGoods")
    public ResultVO getStockGoods(){
        ResultVO stockGoods = stockService.getStockGoods();
        return stockGoods;
    }

    @GetMapping("/getInGoods")
    public ResultVO getInGoods(){
        ResultVO inGoods = inGoodsService.getInGoods();
        return inGoods;
    }

    @PostMapping("/updateGoodsUp")
    public boolean updateGoodsUp(String stockId){
        //修改商品状态
        boolean b = stockService.updateGoodsUp(stockId);
        //查出商品，然后放入队列
        GoodsDto goods = stockService.getGoods(stockId);
        rabbitTemplate.convertAndSend("directGoodsUpShowExchange","goodsUpShow",goods);
        return b;
    }

    @PostMapping("/updateGoodsDown")
    public boolean updateGoodsDown(String stockId){
        //修改商品状态
        boolean b = stockService.updateGoodsDown(stockId);
        //查出商品，然后放入队列
        GoodsDto goods = stockService.getGoods(stockId);
        goods.setGoodTag(0);
        rabbitTemplate.convertAndSend("directGoodsUpShowExchange","goodsUpShow",goods);

        return b;
    }

}
